﻿Imports Microsoft.Win32

Public Class ucSettings

    Public Event SettingsChanged(sender As Object, e As SettingsChangedEventArgs)

    Private isLoading As Boolean = True

    Private Sub ucSettings_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
        isLoading = True

        txtBackground.Text = My.Settings.BackgroundImage

        chkPlaySounds.IsChecked = My.Settings.BackgroundSoundEnabled
        lstPlayMinutes.SelectedValue = My.Settings.BackgroundPlayMinutes
        txtSoundFile.Text = My.Settings.BackgroundSound

        chkEnableAlarm.IsChecked = My.Settings.AlarmEnabled
        lstAlarmHour.SelectedValue = CStr(IIf(My.Settings.AlarmTime.Hour > 12 Or My.Settings.AlarmTime.Hour < 1, Math.Abs(My.Settings.AlarmTime.Hour - 12), My.Settings.AlarmTime.Hour))
        lstAlarmMinute.SelectedValue = My.Settings.AlarmTime.Minute.ToString("00")
        lstAlarmAMPM.SelectedIndex = CInt(IIf(My.Settings.AlarmTime.Hour < 12, 0, 1))

        chkAlarmMon.IsChecked = My.Settings.AlarmDayMask.IndexOf("Mon") >= 0
        chkAlarmTue.IsChecked = My.Settings.AlarmDayMask.IndexOf("Tue") >= 0
        chkAlarmWed.IsChecked = My.Settings.AlarmDayMask.IndexOf("Wed") >= 0
        chkAlarmThu.IsChecked = My.Settings.AlarmDayMask.IndexOf("Thu") >= 0
        chkAlarmFri.IsChecked = My.Settings.AlarmDayMask.IndexOf("Fri") >= 0
        chkAlarmSat.IsChecked = My.Settings.AlarmDayMask.IndexOf("Sat") >= 0
        chkAlarmSun.IsChecked = My.Settings.AlarmDayMask.IndexOf("Sun") >= 0

        txtAlarmFile.Text = My.Settings.AlarmSound

        isLoading = False

    End Sub

    Private Sub cmdClose_Click(sender As Object, e As RoutedEventArgs) Handles cmdClose.Click
        Me.FadeOut()

    End Sub

    Private Sub cmdClearBackground_Click(sender As Object, e As RoutedEventArgs) Handles cmdClearBackground.Click
        My.Settings.BackgroundImage = ""
        txtBackground.Text = ""
        RaiseEvent SettingsChanged(Me, New SettingsChangedEventArgs(SettingsChangedEventArgs.ChangedSettingEnum.BackgroundImage))
    End Sub

    Private Sub cmdChooseImage_Click(sender As Object, e As RoutedEventArgs) Handles cmdChooseImage.Click
        Dim dlg As OpenFileDialog

        Try
            dlg = New OpenFileDialog
            With dlg
                .Filter = "Media files|*.mpg;*.jpg;*.avi|All Files|*.*"
                .FilterIndex = 0
                .Multiselect = False
            End With

            If dlg.ShowDialog Then
                txtBackground.Text = dlg.FileName
                My.Settings.BackgroundImage = dlg.FileName
                RaiseEvent SettingsChanged(Me, New SettingsChangedEventArgs(SettingsChangedEventArgs.ChangedSettingEnum.BackgroundImage))
            End If

        Catch ex As Exception
            MsgBox("Could not access file.", MsgBoxStyle.Exclamation Or MsgBoxStyle.OkOnly)

        End Try

    End Sub

    Private Sub chkPlaySounds_Checked(sender As Object, e As RoutedEventArgs) Handles chkPlaySounds.Checked
        If isLoading Then Exit Sub
        My.Settings.BackgroundSoundEnabled = True
        RaiseEvent SettingsChanged(Me, New SettingsChangedEventArgs(SettingsChangedEventArgs.ChangedSettingEnum.BackgroundSoundEnabled))
    End Sub

    Private Sub chkPlaySounds_Unchecked(sender As Object, e As RoutedEventArgs) Handles chkPlaySounds.Unchecked
        If isLoading Then Exit Sub
        My.Settings.BackgroundSoundEnabled = False
        RaiseEvent SettingsChanged(Me, New SettingsChangedEventArgs(SettingsChangedEventArgs.ChangedSettingEnum.BackgroundSoundEnabled))
    End Sub

    Private Sub lstPlayMinutes_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles lstPlayMinutes.SelectionChanged
        If isLoading Then Exit Sub
        My.Settings.BackgroundPlayMinutes = CInt(lstPlayMinutes.SelectedItem)
        RaiseEvent SettingsChanged(Me, New SettingsChangedEventArgs(SettingsChangedEventArgs.ChangedSettingEnum.BackgroundPlayMinutes))
    End Sub

    Private Sub cmdClearSoundFile_Click(sender As Object, e As RoutedEventArgs) Handles cmdClearSoundFile.Click
        My.Settings.BackgroundSound = ""
        txtSoundFile.Text = ""
        RaiseEvent SettingsChanged(Me, New SettingsChangedEventArgs(SettingsChangedEventArgs.ChangedSettingEnum.BackgroundSound))
    End Sub

    Private Sub cmdChooseSoundFile_Click(sender As Object, e As RoutedEventArgs) Handles cmdChooseSoundFile.Click
        Dim dlg As OpenFileDialog

        Try
            dlg = New OpenFileDialog
            With dlg
                .Filter = "Music Files/Playlist|*.mp3;*.wav;*.wpl|All Files|*.*"
                .FilterIndex = 0
                .Multiselect = False
            End With

            If dlg.ShowDialog Then
                txtSoundFile.Text = dlg.FileName
                My.Settings.BackgroundSound = dlg.FileName
                RaiseEvent SettingsChanged(Me, New SettingsChangedEventArgs(SettingsChangedEventArgs.ChangedSettingEnum.BackgroundSound))
            End If

        Catch ex As Exception
            MsgBox("Could not access file.", MsgBoxStyle.Exclamation Or MsgBoxStyle.OkOnly)

        End Try

    End Sub

    Private Sub chkEnableAlarm_Checked(sender As Object, e As RoutedEventArgs) Handles chkEnableAlarm.Checked
        If isLoading Then Exit Sub
        My.Settings.AlarmEnabled = True
        RaiseEvent SettingsChanged(Me, New SettingsChangedEventArgs(SettingsChangedEventArgs.ChangedSettingEnum.AlarmEnabled))
    End Sub

    Private Sub chkEnableAlarm_Unchecked(sender As Object, e As RoutedEventArgs) Handles chkEnableAlarm.Unchecked
        If isLoading Then Exit Sub
        My.Settings.AlarmEnabled = False
        RaiseEvent SettingsChanged(Me, New SettingsChangedEventArgs(SettingsChangedEventArgs.ChangedSettingEnum.AlarmEnabled))
    End Sub

    Private Sub Alarm_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) _
        Handles lstAlarmHour.SelectionChanged, lstAlarmMinute.SelectionChanged, lstAlarmAMPM.SelectionChanged

        If isLoading Then Exit Sub

        If lstAlarmHour.SelectedItem IsNot Nothing AndAlso lstAlarmMinute.SelectedItem IsNot Nothing AndAlso lstAlarmAMPM.SelectedItem IsNot Nothing Then
            My.Settings.AlarmTime = Date.Parse(String.Format("{0}:{1} {2}", lstAlarmHour.SelectedItem, lstAlarmMinute.SelectedItem, lstAlarmAMPM.SelectedItem))
            RaiseEvent SettingsChanged(Me, New SettingsChangedEventArgs(SettingsChangedEventArgs.ChangedSettingEnum.AlarmTime))
        End If

    End Sub

    Private Sub chkAlarm_Checked(sender As Object, e As RoutedEventArgs) _
    Handles chkAlarmMon.Checked, chkAlarmTue.Checked, chkAlarmWed.Checked, chkAlarmThu.Checked, chkAlarmFri.Checked, chkAlarmSat.Checked, chkAlarmSun.Checked, _
    chkAlarmMon.Unchecked, chkAlarmTue.Unchecked, chkAlarmWed.Unchecked, chkAlarmThu.Unchecked, chkAlarmFri.Unchecked, chkAlarmSat.Unchecked, chkAlarmSun.Unchecked

        If isLoading Then Exit Sub

        Dim sb As New System.Text.StringBuilder

        If chkAlarmMon.IsChecked = True Then sb.Append("Mon")
        If chkAlarmTue.IsChecked = True Then sb.Append("Tue")
        If chkAlarmWed.IsChecked = True Then sb.Append("Wed")
        If chkAlarmThu.IsChecked = True Then sb.Append("Thu")
        If chkAlarmFri.IsChecked = True Then sb.Append("Fri")
        If chkAlarmSat.IsChecked = True Then sb.Append("Sat")
        If chkAlarmSun.IsChecked = True Then sb.Append("Sun")

        My.Settings.AlarmDayMask = sb.ToString
        RaiseEvent SettingsChanged(Me, New SettingsChangedEventArgs(SettingsChangedEventArgs.ChangedSettingEnum.AlarmTime))

    End Sub

    Private Sub cmdClearAlarmFile_Click(sender As Object, e As RoutedEventArgs) Handles cmdClearAlarmFile.Click
        My.Settings.AlarmSound = ""
        txtAlarmFile.Text = ""
        RaiseEvent SettingsChanged(Me, New SettingsChangedEventArgs(SettingsChangedEventArgs.ChangedSettingEnum.AlarmSound))

    End Sub

    Private Sub cmdChooseAlarmFile_Click(sender As Object, e As RoutedEventArgs) Handles cmdChooseAlarmFile.Click
        Dim dlg As OpenFileDialog

        Try
            dlg = New OpenFileDialog
            With dlg
                .Filter = "Music Files/Playlist|*.mp3;*.wav;*.wpl|All Files|*.*"
                .FilterIndex = 0
                .Multiselect = False
            End With

            If dlg.ShowDialog Then
                txtAlarmFile.Text = dlg.FileName
                My.Settings.AlarmSound = dlg.FileName
                RaiseEvent SettingsChanged(Me, New SettingsChangedEventArgs(SettingsChangedEventArgs.ChangedSettingEnum.AlarmSound))
            End If

        Catch ex As Exception
            MsgBox("Could not access file.", MsgBoxStyle.Exclamation Or MsgBoxStyle.OkOnly)

        End Try

    End Sub

    Public Sub FadeIn()
        Dim sb As New Animation.Storyboard
        Dim ani As Animation.DoubleAnimation

        If Me.Opacity <> 0 Then Exit Sub

        sb = New Animation.Storyboard
        ani = New Animation.DoubleAnimation

        With ani
            .From = 0
            .To = 0.9
            .Duration = New Duration(New TimeSpan(0, 0, 0, 0, 500))
        End With

        Animation.Storyboard.SetTarget(ani, Me)
        Animation.Storyboard.SetTargetProperty(ani, New PropertyPath("Opacity"))

        With sb
            .Children.Add(ani)
            .Begin()
        End With
    End Sub

    Public Sub FadeOut()
        Dim sb As New Animation.Storyboard
        Dim ani As Animation.DoubleAnimation

        If Me.Opacity <> 0.9 Then Exit Sub

        sb = New Animation.Storyboard
        ani = New Animation.DoubleAnimation

        With ani
            .From = 0.9
            .To = 0
            .Duration = New Duration(New TimeSpan(0, 0, 0, 0, 500))
        End With

        Animation.Storyboard.SetTarget(ani, Me)
        Animation.Storyboard.SetTargetProperty(ani, New PropertyPath("Opacity"))

        With sb
            .Children.Add(ani)
            .Begin()
        End With

    End Sub

    Public Class SettingsChangedEventArgs
        Inherits EventArgs

        Public Enum ChangedSettingEnum
            BackgroundImage
            BackgroundSoundEnabled
            BackgroundSound
            BackgroundPlayMinutes
            AlarmEnabled
            AlarmTime
            AlarmSound 
        End Enum

        Public ChangedSetting As ChangedSettingEnum

        Public Sub New()

        End Sub

        Public Sub New(setting As ChangedSettingEnum)
            Me.ChangedSetting = setting
        End Sub

    End Class


End Class
